package cn.ghf.basic.util;

import cn.ghf.user.domain.LoginInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

public class LoginContext {
    public static LoginInfo getLogin(HttpServletRequest request){
        String token = request.getHeader("token");
        if(!StringUtils.isEmpty(token)){
            //获取spring容器中的redis，首先要获取容器
            WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
            //从容器中获取redis对象
            RedisTemplate redisTemplate = (RedisTemplate)webApplicationContext.getBean("redisTemplate");
            //从redis中获取用户信息
            Object loginInfo = redisTemplate.opsForValue().get(token);
            if(loginInfo!=null){
                return (LoginInfo)loginInfo;
            }
        }
        return null;
    }
}
